<template>
    <layout>
        <div class='wrapper'>
            <van-form @submit="onSubmit">
                <van-field
                    name="name"
                    label="姓名"
                    v-model="formData.name"
                    placeholder="请输入姓名"
                    @input="$Utils.validateValue(formData,'name')"
                    required
                    :rules="[{required:true,message:'请输入姓名'}]">

                </van-field>
                <van-field
                    name="idCard"
                    label="身份证"
                    v-model="formData.idCard"
                    placeholder="请输入身份证"
                    required
                    :rules="[{required:true,message:'请输入身份证'},{validator:$Utils.validateIdCard(formData,'idCard'),message:'身份证格式错误'}]">
                </van-field>

                <van-field
                    name="phone"
                    label="联系电话"
                    v-model="formData.phone"
                    placeholder="请输入联系电话"
                    required
                    :rules="[{required:true,message:'请输入联系电话'}]">
                </van-field>
                <van-field
                    name="address"
                    label="住址"
                    v-model="formData.address"
                    placeholder="请输入住址"
                   >
                </van-field>

                <van-field
                    name="certificateFlag"
                    label="专业护士证"
                    >
                    <template #input>
                        <van-radio-group v-model="formData.certificateFlag" direction="horizontal">
                            <van-radio :name="1">
                                是
                            </van-radio>
                            <van-radio :name="2">
                                否
                            </van-radio>
                        </van-radio-group>
                    </template>
                </van-field>
                <van-field
                    name="nurseLevel"
                    label="等级"
                    v-model="formData.nurseLevel"
                    placeholder="请输入护士证等级">

                </van-field>
                <van-field
                    name="remark"
                    label="备注"
                    type="textarea"
                    rows="2"
                    autosize
                    v-model="formData.remark"
                    placeholder="备注"
                    maxlength="50"
                    clearable
                    show-word-limit>

                </van-field>
                <div class="btn">
                    <van-button  plain type="info" native-type="button" @click="returns">取 消</van-button>
                    <van-button  type="info" native-type="submit">确 定</van-button>
                </div>
            </van-form>
        </div>
    </layout>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import addNurseApi from '@/api/addNurse'
export default {
    // import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
        // 这里存放数据
        return {
            openId:"",
            formData:{
                name:"",
                idCard:"",
                phone:"",
                address:"",
                certificateFlag:1,
                nurseLevel:"",
                remark:"",
            }
        }
    },
    // 监听属性 类似于data概念
    computed: {},
    // 监控data中的数据变化
    watch: {},
    // 方法集合
    methods: {
        init(){
            if(this.$route.query.openId){
                this.openId = this.$route.query.openId
            }else{
                this.$dialog.alert({
                    title:"提示",
                    message:"请退出页面，重新进入，否者此次操作无效！！"
                }).then(res=>{
                    window.close();
                    WeixinJSBridge.call('closeWindow');
                    return;
                })

            }
        },
        returns(){
            // window.opener=null;
            // window.open('','_self');
            window.close();
            WeixinJSBridge.call('closeWindow');
        },
        onSubmit(){
            this.formData.name =  this.formData.name.replace(/\s/g,"")
            let params = {
                nurseName:this.formData.name,
                idCard:this.formData.idCard,
                phone:this.formData.phone,
                address:this.formData.address,
                certificateFlag:Number(this.formData.certificateFlag),
                nurseLevel:this.formData.nurseLevel,
                remark:this.formData.remark,
                openId:this.openId,
            }

            this.$dialog.confirm({
                title:"提示",
                message:"确定提交？"
            }).then(res=>{
                addNurseApi.nurseRegister(params)
                .then(res=>{
                    if(res.status==0){
                        this.$toast({
                            type:"success",
                            message:res.msg
                        })
                        setTimeout(()=>{
                            this.$router.push({
                                path:"/sampleTask",
                                query:{
                                    openId:this.openId
                                }
                            })
                        },2000)
                    }else{
                        this.$dialog.alert({
                            title:"提示",
                            message:res.msg
                        })
                        return
                    }
                })
                .catch(err=>{
                    this.$dialog.alert({
                        title:"错误",
                        message:err
                    })
                })
            })

        }
    },
    // 生命周期 - 创建完成（可以访问当前this实例）
    created() {

    },
    // 生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
        this.init()
    },
    beforeCreate() { }, // 生命周期 - 创建之前
    beforeMount() { }, // 生命周期 - 挂载之前
    beforeUpdate() { }, // 生命周期 - 更新之前
    updated() { }, // 生命周期 - 更新之后
    beforeDestroy() { }, // 生命周期 - 销毁之前
    destroyed() { }, // 生命周期 - 销毁完成
    activated() { } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="less" scoped>
    .wrapper{
        width: 93%;
        margin: auto;
        border-radius: 10px;
        background-color: white;
        overflow: hidden;
        box-shadow: 0 0 12px rgba(0,0,0,10%);
        .btn{
            display: flex;
            justify-content: space-between;
            padding: 20px;
            .van-button{
                width: 120px;
                height: 40px;
                border-radius: 10px;
            }
        }
    }
</style>
